<template>
  <el-dialog
    title="问题类型管理"
    :visible.sync="isshow"
    width="700px"
  >
    <!--    操作按钮-->
    <div class="btns">
      <div>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addType()">新增</el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multipleSelection.length==0" @click="moredelType()">
          删除
        </el-button>
      </div>
    </div>
    <!--    表格-->

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange" id="table_count">
      <el-table-column
        type="selection"
        width="55"
      >
      </el-table-column>
      <el-table-column label="排序" align="center" prop="type" width="55">
        <template slot-scope="scope">
          <div style="cursor: pointer">
            <i class="el-icon-sort handle"></i>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="序号" align="center" prop="sort" width="55"/>
      <el-table-column label="问题类型" align="center" prop="faqTypeName">
        <template slot-scope="scope">
          <!--          <input type="text" v-model="scope.row.type" :disabled="!scope.row.update" :class="scope.row.update? 'update':'confirm'">-->
          <el-input v-model="scope.row.faqTypeName" :disabled="!scope.row.update" :class="scope.row.update? 'update':'confirm'"></el-input>

        </template>
      </el-table-column>
      >
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            @click="UpdateType(scope.row.faqTypeId)"
          >{{ scope.row.update ? '完成' : '修改' }}
          </el-button
          >
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            @click="handleDel(scope.row)"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
    <el-button @click="isshow = false">取 消</el-button>
    <el-button type="primary" @click="saveType()">保 存</el-button>
  </span>
  </el-dialog>
</template>
<script>

import Sortable from 'sortablejs'
import { getfaqTypelist, addfaqType, updatefaqType, deletefaqType, sortfaqType } from '@/api/commonproblem/problem'

export default {
  name: 'questionType',
  data() {
    return {
      isshow: false,
      loading: '',
      postList: [
        {
          id: 1,
          sort: 1,
          type: '课程创建'
        },
        {
          id: 2,
          sort: 2,
          type: '其他'
        },
        {
          id: 3,
          sort: 3,
          type: '其他4'
        }
      ],
      multipleSelection: [],
      newList: [],
      sortable: void 0
    }
  },
  mounted() {
  },
  methods: {
    show() {
      this.getTypelist()
      this.isshow = true
      this.$nextTick(() => {
        this.rowDrop()
      })

    },
    // 获取问题类型
    async getTypelist() {
      let res = await getfaqTypelist()
      if (res.code == 200) {
        this.postList = res.data.map((item) => {
          return {
            ...item,
            update: false,
            additem: false
          }
        })
      }

    },
    // 新增
    async confirmaddType(name) {
      let res = await addfaqType({ faqTypeName: name })
      if (res.code == 200) {
        this.$message.success('新增成功')
        this.getTypelist()
      } else {
        this.$message.error('新增失败')
        this.getTypelist()
      }
      this.postList.forEach(() => {

      })
    },
    // 编辑
    UpdateType(faqTypeId) {
      console.log(this.postList, 'additem')

      this.postList.forEach(e => {

        if (e.update === true && e.faqTypeName == '') {
          this.$message.error('请输入问题类型名称')
          return
        }
        if (e.additem === true) {
          this.confirmaddType(e.faqTypeName)
        }
        if (e.update === true && e.additem === false) {
          // 修改
          let obj = {
            faqTypeId: e.faqTypeId,
            faqTypeName: e.faqTypeName
          }
          this.setType(obj)
        }
        if (e.faqTypeId === faqTypeId) {
          this.sortable.options.disabled = !e.update
          e.update = !e.update
        }
      })
    },
    // 添加
    addType() {
      this.postList.push({
        faqTypeId: this.postList.length + 1,
        sort: this.postList.length + 1,
        update: true,
        additem: true,
        faqTypeName: ''

      })
      this.sortable.options.disabled = true
    },
    // 修改
    async setType(obj) {
      let res = await updatefaqType(obj)
      if (res.code == 200) {
        this.$message.success('修改成功')
        this.getTypelist()
      } else {
        this.$message.error('修改失败')
        this.getTypelist()
      }
    },
    // 删除
    handleDel(row) {
      this.$confirm(`"${row.faqTypeName}"类型删除后对应问题类型将变成“其他”，确认将其删除吗？`, '删除确认').then(() => {
        this.conforDel(row.faqTypeId)

      }).catch(() => {
        this.$message.info('已取消删除')

      })

    },
    // 确认删除
    async conforDel(ids) {
      let arr = [ids]
      let res = await deletefaqType(arr.join(','))
      if (res.code == 200) {
        this.$message.success('删除成功')
        this.getTypelist()
        this.$parent.getquestionList()
      } else {
        this.$message.error('删除失败')
        this.getTypelist()
        this.$parent.getquestionList()
      }

    },
    // 保存
    saveType() {
      let num = 0
      let finsh = 0
      this.postList.forEach(e => {
        if (e.faqTypeName == '') {
          num++
        }
        if (e.update == true) {
          finsh++
        }
      })
      if (num > 0) {
        this.$message.error('部分问题类型名称未填写，请检查')
        return
      }
      if (finsh > 0) {
        this.$message.error('有问题类型未编辑完成，请检查')
        return
      }
      this.isshow = false
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 批量删
    moredelType() {
      this.$confirm('确认删除选中问题类型吗？', '删除确认').then(() => {
        let arr = this.multipleSelection.map(e => {
          return e.faqTypeId
        })
        this.conforDel(arr.join(','))
      }).catch(() => {
        this.$message.info('已取消删除')
      })

    },
    // 表格行拖拽
    rowDrop() {
      const el = document.querySelector('#table_count .el-table__body-wrapper tbody')
      this.sortable = new Sortable(el, {
        animation: 150,
        handle: '.handle',
        ghostClass: 'blue-background-class',
        disabled: false,
        onEnd: (/**Event*/evt) => {
          this.setNodeSort(evt.oldIndex, evt.newIndex)
        }
      })
    },
    // 拖拽完成更新数据
    setNodeSort(oldIndex, newIndex) {
      const arr = JSON.parse(JSON.stringify(this.postList))
      const currRow = arr.splice(oldIndex, 1)[0]
      arr.splice(newIndex, 0, currRow)
      this.postList = []
      this.$nextTick(() => {
        this.postList = JSON.parse(JSON.stringify(arr))
        this.postList.forEach((item, index) => {
          item.sort = index + 1
        })
        this.sortfaqTypeList()
      })
    },
    // 排序
    async sortfaqTypeList() {
      let res = await sortfaqType({ ccFaqTypeList: this.postList })
      if (res.code == 200) {
        this.$message.success('排序成功')
        this.getTypelist()
      }

    }
  }
}
</script>
<style scoped lang="scss">
.btns {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 0 10px 0;
}

::v-deep.el-input.is-disabled .el-input__inner {
  border: 0 !important;
  background: transparent !important;
  color: #000000;
  text-align: center;
}
</style>
